//
//  AlarmListVC.m
//  efairy
//  报警列表
//  Created by 李锋 on 2019/2/22.
//  Copyright © 2019年 East. All rights reserved.
//

#import "AlarmListVC.h"
#import <MJRefresh.h>
#import "EFAModel.h"
#import "ApiManager.h"
#import "AlarmListModel.h"
#import "GlobalHeader.h"
#import "AlarmListCell.h"
#import "AlarmDisposeCell.h"
#import "AlarmDisposeDetailVC.h"
#import "AlarmDisposeVC.h"

@interface AlarmListVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;//未处理数据tableview
@property(nonatomic,strong)UITableView *disTableView;//已处理数据tableview
@property(nonatomic,strong)UIView *chooseTypeView;//选择报警类型  全部，火警，预警，故障
@property(nonatomic,strong)UIView *disposeTypeVieew;//选择“是否已处理”报警类型
@property(nonatomic,strong)UILabel *alarmLable;
@property(nonatomic,strong)UILabel *disposeLable;
@property(nonatomic,strong)UIButton *submitBtn;
@property(nonatomic,strong)UIButton *allChooseBtn;
@property (nonatomic, strong) MJRefreshComponent *refreshViewComponent;
@property(nonatomic,assign) int pageIndex;
@property(nonatomic,assign) int disposePageIndex;
@property(nonatomic,assign) NSInteger lastId;
@property(nonatomic,strong) NSString *disposeLastId;
@property(nonatomic,assign) int alarmType;
@property(nonatomic,assign) BOOL isDispose;
@property(nonatomic,copy) NSMutableArray<AlarmNoDispostListModel *> *noDisposeArray;//保存未处理数据
@property(nonatomic,copy) NSMutableSet<NSNumber *> *noChooseIds;//保存点击了全选按钮然后又手动取消选择单个选项的报警编号
@property(nonatomic,copy) NSMutableArray<AlarmDispostListModel *> *disposeArray;//保存已处理数据
@property(nonatomic,assign) BOOL isChooseAll;//是否全选
@property(nonatomic,assign) BOOL isOnClickChooseBtn;//是否主动点击 全选/全不选按钮；主动点击全选为yes ；全不选为false；
@property(nonatomic,assign) int checkedRow;//被选中的条数
@property(nonatomic,assign) int allRows;//未处理一共有多少条数据
@property(nonatomic,assign) int disposeAllRows;//已处理一共有多少条数据
@property(nonatomic,strong) UIView *typeListView;
@property(nonatomic,strong) UIView *disposeListView;
//@property(nonatomic,strong) NSArray *alarmType;
//@property(nonatomic,strong) NSArray *alarmTypeValue;

@property(nonatomic,strong) NSMutableArray<UILabel *> *typeUILabel;
@property(nonatomic,strong) NSMutableArray<UIImageView *> *typeImageView;


@end

@implementation AlarmListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationbar];
    _isDispose = NO;
    _alarmType = 0;
    _noChooseIds = [[NSMutableSet alloc]init];
    [self initView];
}

-(void)initNavigationbar{
    self.navigationItem.title = @"报警列表";
}

-(void)initView{
    float  navigationbarH = self.navigationController.navigationBar.frame.size.height;

    self.view .backgroundColor =  ColorFromRGB(0xeff0f4);
    
    _chooseTypeView = [[UIView alloc]initWithFrame:CGRectMake(0, statusBarHeight+navigationbarH, SCREEN_WIDTH/2, 40)];//选择报警类型
     [self.view addSubview:_chooseTypeView];
    _chooseTypeView.backgroundColor = [UIColor whiteColor];
    _alarmLable = [[UILabel alloc]init];
    _alarmLable.text =@"全部";
    _alarmLable.textColor = ColorFromRGB(0x282449);
    _alarmLable.font = [UIFont systemFontOfSize:16.f];
    _alarmLable.textAlignment = NSTextAlignmentCenter;
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"icon_arrow_bottom"];
    
    UITapGestureRecognizer *tapType=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickType:)];
    [_chooseTypeView addGestureRecognizer:tapType];
    
    [_chooseTypeView sd_addSubviews:@[_alarmLable,imageView]];
    _alarmLable.sd_layout.heightIs(16).widthIs(33)
    .topSpaceToView(_chooseTypeView, 12)
    .leftSpaceToView(_chooseTypeView, (SCREEN_WIDTH/2-4-49)/2);
    imageView.sd_layout.widthIs(13)
    .heightIs(8)
    .topSpaceToView(_chooseTypeView, 16)
    .leftSpaceToView(_alarmLable, 6);
    
  
    
    
    
    _disposeTypeVieew = [[UIView alloc]initWithFrame:
                         CGRectMake(SCREEN_WIDTH/2, statusBarHeight+navigationbarH, SCREEN_WIDTH/2, 40)];//选择是否报警
    _disposeTypeVieew.backgroundColor = [UIColor whiteColor];
   [self.view addSubview:_disposeTypeVieew];
    

    _disposeLable = [[UILabel alloc]init];
    _disposeLable.text = @"未处理";
    _disposeLable.textColor = ColorFromRGB(0x282449);
    _disposeLable.font = [UIFont systemFontOfSize:16.f];
    
    UIView *hLine = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, statusBarHeight+navigationbarH+9, 2, 25)];//线条
    hLine.backgroundColor  = ColorFromRGB(0xe5e5e5);
    [self.view addSubview:hLine];

    UIImageView *imageView2 = [[UIImageView alloc]init];
    imageView2.image = [UIImage imageNamed:@"icon_arrow_bottom"];
    
    UITapGestureRecognizer *disposeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickIsDispose:)];
    [_disposeTypeVieew addGestureRecognizer:disposeTap];
    
    [_disposeTypeVieew sd_addSubviews:@[_disposeLable,imageView2]];

    
    
    _disposeLable.sd_layout.heightIs(16).widthIs(49)
    .topSpaceToView(_disposeTypeVieew, 12)
    .leftSpaceToView(_disposeTypeVieew, (SCREEN_WIDTH/2-4-65)/2);
    imageView2.sd_layout.widthIs(13) .heightIs(8)
    .topSpaceToView(_disposeTypeVieew, 16)
    .leftSpaceToView(_disposeLable, 6);
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, statusBarHeight+navigationbarH+41, SCREEN_WIDTH, 1)];//线条
    line.backgroundColor =ColorFromRGB(0xefefef);
     [self.view addSubview:line];
    
//    UIView *contentView  = [[UIView alloc]initWithFrame:CGRectMake(0,  statusBarHeight+navigationbarH+42, SCREEN_WIDTH, SCREEN_HEIGHT-(statusBarHeight+navigationbarH+42+50))];
//    contentView.backgroundColor =ColorFromRGB(0xeff0f4);
//    [self.view addSubview:contentView];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,statusBarHeight+navigationbarH+40, SCREEN_WIDTH, SCREEN_HEIGHT-(statusBarHeight+navigationbarH+42+50))];
    self.tableView.backgroundColor = ColorFromRGB(0xeff0f4);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = ColorFromRGB(0xeff0f4);//设置分割线颜色
    //多余分割线隐藏
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    __weak typeof(self) weakSelf = self;
    //..下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.refreshViewComponent = weakSelf.tableView.mj_header;
        weakSelf.pageIndex = 1;
        weakSelf.isOnClickChooseBtn = NO;
        weakSelf.isChooseAll = NO;
        [weakSelf.noChooseIds removeAllObjects];
        weakSelf.checkedRow = 0;
        [weakSelf setAllChooseBtnStatus:NO androws:weakSelf.checkedRow];
        [weakSelf requestNoDisposeListWithHeader:YES];
    }];
    
    //..上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refreshViewComponent = weakSelf.tableView.mj_footer;
        weakSelf.pageIndex = weakSelf.pageIndex + 1;
        [weakSelf requestNoDisposeListWithHeader:NO];
    }];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    
    
    UIView *bottonView  = [[UIView alloc]initWithFrame:CGRectMake(0,  SCREEN_HEIGHT-50, SCREEN_WIDTH,50)];
    bottonView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottonView];
    
    _submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-82,  SCREEN_HEIGHT-41, 72,32)];
    [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [_submitBtn setBackgroundColor:ColorFromRGB(0x282449)];
    _submitBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    _submitBtn.layer.cornerRadius = 12;
    [_submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_submitBtn];
    
    
    //开始创建左边按钮
    _allChooseBtn =  [[UIButton alloc]initWithFrame:CGRectMake(21,SCREEN_HEIGHT-35, 180,21)];
    [_allChooseBtn setTitle:@"全选(已选中0条)" forState:UIControlStateNormal];
    _allChooseBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _allChooseBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_allChooseBtn setTitleColor:ColorFromRGB(0x333333) forState:UIControlStateNormal];
    [_allChooseBtn setImage:[UIImage imageNamed:@"icon_no_checked"] forState:UIControlStateNormal];
    [_allChooseBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -6, 0, 0)];//设置图片在button的位置

    [_allChooseBtn addTarget:self action:@selector(allCheckedBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_allChooseBtn];
    
    self.disTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,statusBarHeight+navigationbarH+40, SCREEN_WIDTH, SCREEN_HEIGHT-(statusBarHeight+navigationbarH)) style:UITableViewStyleGrouped];
    
    
    self.disTableView.backgroundColor = ColorFromRGB(0xeff0f4);
    self.disTableView.delegate = self;
    self.disTableView.dataSource = self;
//    self.disTableView.separatorColor = ColorFromRGB(0xeff0f4);//设置分割线颜色
    self.disTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //多余分割线隐藏
//    self.disTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.disTableView.hidden = YES;
    [self.view addSubview:self.disTableView];
 
 
    
    //..下拉刷新
    self.disTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.refreshViewComponent = weakSelf.disTableView.mj_header;
        weakSelf.disposePageIndex = 1;
        [weakSelf requestDisposeListWithHeader:YES];
    }];
    
    //..上拉刷新
    self.disTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refreshViewComponent = weakSelf.disTableView.mj_footer;
        weakSelf.pageIndex = weakSelf.disposePageIndex + 1;
        [weakSelf requestDisposeListWithHeader:NO];
    }];
       [self.view addSubview:_disTableView];
//    self.disTableView.sd_layout
//    .leftSpaceToView(self.view, 8)
//    .rightSpaceToView(self.view, 8);
//    .topSpaceToView(_chooseTypeView, 8);
    
    [self initTypeView:navigationbarH];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];//马上刷新
    
}


-(void)initTypeView:(float)navigationbarH{
//    _alarmType = @[@"全部",@"火警",@"预警",@"故障"];
//    _alarmTypeValue = @[@"-1",@"1",@"2",@"3"];
    _typeUILabel = [[NSMutableArray alloc]init];
    _typeImageView = [[NSMutableArray alloc]init];
    
    _typeListView = [[UIView alloc]initWithFrame:CGRectMake(0, statusBarHeight+navigationbarH+40, SCREEN_WIDTH, 165)];
    _typeListView.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    UIView *uiView =  [[UIView alloc]init];
    uiView.tag = 0;
    UITapGestureRecognizer *viewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickTypeItem:)];
    [uiView addGestureRecognizer:viewTap];
    
    uiView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]init];
    label.textColor = ColorFromRGB(0x282449);
    label.font = [UIFont systemFontOfSize:14.f];
    label.text = @"全部";
    [_typeUILabel addObject:label];
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"icon_select_right"];
    [_typeImageView addObject:imageView];
    [uiView sd_addSubviews:@[label,imageView]];
    label.sd_layout.widthIs(142).heightIs(14)
    .topSpaceToView(uiView, 13)
    .leftSpaceToView(uiView, 12);
    
    imageView.sd_layout.widthIs(14.1).heightIs(10)
    .rightSpaceToView(uiView, 12)
    .topSpaceToView(uiView, 15);
    
    
    
    UIView *uiView2 =  [[UIView alloc]init];
    uiView2.tag = 1;
    UITapGestureRecognizer *viewTap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickTypeItem:)];
    [uiView2 addGestureRecognizer:viewTap2];
    uiView2.backgroundColor = [UIColor whiteColor];
    
    UILabel *label2 = [[UILabel alloc]init];
    label2.textColor = ColorFromRGB(0x666666);
    label2.font = [UIFont systemFontOfSize:14.f];
    label2.text = @"火警";
    [_typeUILabel addObject:label2];
    UIImageView *imageView2 = [[UIImageView alloc]init];
    imageView2.image = nil;
    [_typeImageView addObject:imageView2];
    [uiView2 sd_addSubviews:@[label2,imageView2]];
    
    label2.sd_layout.widthIs(142).heightIs(14)
    .topSpaceToView(uiView2, 13)
    .leftSpaceToView(uiView2, 12);
    
    imageView2.sd_layout.widthIs(14.1).heightIs(10)
    .rightSpaceToView(uiView2, 12)
    .topSpaceToView(uiView2, 15);
    
    
    
    UIView *uiView3 =  [[UIView alloc]init];
    uiView3.tag = 2;
    UITapGestureRecognizer *viewTap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickTypeItem:)];
    [uiView3 addGestureRecognizer:viewTap3];
    
    uiView3.backgroundColor = [UIColor whiteColor];
    UILabel *label3 = [[UILabel alloc]init];
    label3.textColor = ColorFromRGB(0x666666);
    label3.font = [UIFont systemFontOfSize:14.f];
    label3.text = @"预警";
    [_typeUILabel addObject:label3];
    UIImageView *imageView3 = [[UIImageView alloc]init];
    imageView3.image = nil;
    [_typeImageView addObject:imageView3];
    [uiView3 sd_addSubviews:@[label3,imageView3]];
    label3.sd_layout.widthIs(142).heightIs(14)
    .topSpaceToView(uiView3, 13)
    .leftSpaceToView(uiView3, 12);
    
    imageView3.sd_layout.widthIs(14.1).heightIs(10)
    .rightSpaceToView(uiView3, 12)
    .topSpaceToView(uiView3, 15);
    
    
    
    
    UIView *uiView4 =  [[UIView alloc]init];
    uiView4.tag = 3;
    UITapGestureRecognizer *viewTap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickTypeItem:)];
    [uiView4 addGestureRecognizer:viewTap4];
    
    uiView4.backgroundColor = [UIColor whiteColor];
    UILabel *label4 = [[UILabel alloc]init];
    label4.textColor = ColorFromRGB(0x666666);
    label4.font = [UIFont systemFontOfSize:14.f];
    label4.text = @"故障";
    [_typeUILabel addObject:label4];
    UIImageView *imageView4 = [[UIImageView alloc]init];
    imageView4.image = nil;
    [uiView4 sd_addSubviews:@[label4,imageView4]];
    [_typeImageView addObject:imageView4];
    label4.sd_layout.widthIs(142).heightIs(14)
    .topSpaceToView(uiView4, 13)
    .leftSpaceToView(uiView4, 12);
    
    imageView4.sd_layout.widthIs(14.1).heightIs(10)
    .rightSpaceToView(uiView4, 12)
    .topSpaceToView(uiView4, 15);
    
    [_typeListView sd_addSubviews:@[uiView,uiView2,uiView3,uiView4]];
    
    uiView.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40)
    .topSpaceToView(_typeListView, 1);
    
    uiView2.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40)
    .topSpaceToView(uiView, 1);
    
    uiView3.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40)
    .topSpaceToView(uiView2, 1);
    
    uiView4.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40)
    .topSpaceToView(uiView3, 1);
    
    [self.view addSubview:_typeListView];
    
    
    _disposeListView = [[UIView alloc]initWithFrame:CGRectMake(0, statusBarHeight+navigationbarH+40, SCREEN_WIDTH, 83)];
    _disposeListView.backgroundColor = ColorFromRGB(0xe5e5e5);
    UIView *dUIView =  [[UIView alloc]init];
    dUIView.tag = 4;
    UITapGestureRecognizer *dUIViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickTypeItem:)];
    [dUIView addGestureRecognizer:dUIViewTap];
    
    dUIView.backgroundColor = [UIColor whiteColor];
    UILabel *dLabel = [[UILabel alloc]init];
    dLabel.textColor = ColorFromRGB(0x282449);
    dLabel.font = [UIFont systemFontOfSize:14.f];
    dLabel.text = @"未处理";
    [_typeUILabel addObject:dLabel];
    UIImageView *dImageView = [[UIImageView alloc]init];
    dImageView.image = [UIImage imageNamed:@"icon_select_right"];
    [_typeImageView addObject:dImageView];
    [dUIView sd_addSubviews:@[dLabel,dImageView]];
    dLabel.sd_layout.widthIs(142).heightIs(14)
    .topSpaceToView(dUIView, 13)
    .leftSpaceToView(dUIView, 12);
    
    dImageView.sd_layout.widthIs(14.1).heightIs(10)
    .rightSpaceToView(dUIView, 12)
    .topSpaceToView(dUIView, 15);
    
    UIView *dUIView2 =  [[UIView alloc]init];
    dUIView2.tag = 5;
    UITapGestureRecognizer *dUIViewTap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickTypeItem:)];
    [dUIView2 addGestureRecognizer:dUIViewTap2];
    
    dUIView2.backgroundColor = [UIColor whiteColor];
    UILabel *dLabel2 = [[UILabel alloc]init];
    dLabel2.textColor = ColorFromRGB(0x666666);
    dLabel2.font = [UIFont systemFontOfSize:14.f];
    dLabel2.text = @"已处理";
    [_typeUILabel addObject:dLabel2];
    UIImageView *dImageView2 = [[UIImageView alloc]init];
    dImageView2.image = nil;
    [_typeImageView addObject:dImageView2];
    [dUIView2 sd_addSubviews:@[dLabel2,dImageView2]];
    dLabel2.sd_layout.widthIs(142).heightIs(14)
    .topSpaceToView(dUIView2, 13)
    .leftSpaceToView(dUIView2, 12);
    
    dImageView2.sd_layout.widthIs(14.1).heightIs(10)
    .rightSpaceToView(dUIView2, 12)
    .topSpaceToView(dUIView2, 15);
    [_disposeListView sd_addSubviews:@[dUIView,dUIView2]];
    dUIView.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40)
    .topSpaceToView(_disposeListView, 1);
    
    dUIView2.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40)
    .topSpaceToView(dUIView, 1);
    [self.view addSubview:_disposeListView];
    _typeListView.hidden = YES;
    _disposeListView.hidden = YES;
}

//设置选中的报警类型和选中已处理/未处理的图标样式
-(void)setChooseItemImage:(int)index andcount:(int)count{
    if(count == 4){
        for(int i = 0 ;i < count; i++){
            if(index == i){
                _typeUILabel[i].textColor = ColorFromRGB(0x282449);
                _typeImageView[i].image = [UIImage imageNamed:@"icon_select_right"];
            }else{
                _typeUILabel[i].textColor = ColorFromRGB(0x666666);
                _typeImageView[i].image = nil;
            }
        }
    }else if(count == 6){
        for(int i = 4 ;i < count; i++){
            if(index == i){
                _typeUILabel[i].textColor = ColorFromRGB(0x282449);
                _typeImageView[i].image = [UIImage imageNamed:@"icon_select_right"];
            }else{
                _typeUILabel[i].textColor = ColorFromRGB(0x666666);
                _typeImageView[i].image = nil;
            }
        }
    }
   
}

//报警类型 或者 已处理/未处理 选项被点击
-(void)onClickTypeItem:(id)tap{
    UITapGestureRecognizer *uitap = (UITapGestureRecognizer*)tap;
    UIView *views = (UIView*) uitap.view;
    NSInteger tag = views.tag;
    switch (tag) {
        case 0:
            _alarmType = 0;
            _typeListView.hidden = YES;//隐藏报警类型选择页
            _alarmLable.text = @"全部";
            [self setChooseItemImage:0 andcount:4];
            [self headerBeginRefresh];
            break;
        case 1:
            _alarmType = 1;
            _typeListView.hidden = YES;
            _alarmLable.text = @"火警";
            [self setChooseItemImage:1 andcount:4];
            [self headerBeginRefresh];
            break;
        case 2:
            _alarmType = 2;
            _typeListView.hidden = YES;
            _alarmLable.text = @"预警";
            [self setChooseItemImage:2 andcount:4];
            [self headerBeginRefresh];
            break;
        case 3:
            _alarmType = 3;
            _typeListView.hidden = YES;
            _alarmLable.text = @"故障";
            [self setChooseItemImage:3 andcount:4];
            [self headerBeginRefresh];
            break;
        case 4:
            _isDispose = NO;
            self.disTableView.hidden = YES;//隐藏已处理tableview，隐藏已处理的数据
            _disposeListView.hidden  = YES;//隐藏已处理/未处理选择页
            _disposeLable.text = @"未处理";
            [self setChooseItemImage:4 andcount:6];
//            if(_noDisposeArray == nil || _noDisposeArray.count == 0){
//                [self.tableView.mj_header beginRefreshing];//马上刷新
//            }
            break;
        case 5:
            _isDispose = YES;
            self.disTableView.hidden = NO;//显示已处理tableview用于展示数据
            _disposeListView.hidden  = YES;//隐藏已处理/未处理选择页
            _disposeLable.text = @"已处理";
            [self setChooseItemImage:5 andcount:6];
            if(_disposeArray == nil || _disposeArray.count == 0){
                [self.disTableView.mj_header beginRefreshing];//马上刷新
            }
            break;
        default:
            break;
    }
}


//开始自动下拉刷新  用于选择报警类型后执行
-(void)headerBeginRefresh{
    if(_isDispose){
        [self.disTableView.mj_header beginRefreshing];//马上刷新
    }else{
        [self.tableView.mj_header beginRefreshing];//马上刷新
    }
}

//点击报警类型选择栏
-(void)onClickType:(id)tap{
    _typeListView.hidden = !_typeListView.hidden;
    _disposeListView.hidden  = YES;
}

//点击是否已处理选择栏
-(void)onClickIsDispose:(id)tap{
     _typeListView.hidden = YES;
    _disposeListView.hidden = !_disposeListView.hidden;
}


//提交
-(void)submitBtnClick{
   
    
    
    AlarmDisposeVC *vc = [[AlarmDisposeVC alloc]init];
    vc.device_id = self.deviceId;
    vc.isOnClickChooseBtn = _isOnClickChooseBtn;//是否点击了《全选》按钮
    if(!_isOnClickChooseBtn){
        NSMutableArray<AlarmNoDispostListModel *> *chooseModel = [[NSMutableArray alloc]init];
        NSMutableArray<NSNumber *> *chooseAarmIds = [[NSMutableArray alloc]init];
        if(_noDisposeArray && _noDisposeArray.count > 0){
            for(int i = 0 ; i<_noDisposeArray.count; i++){
                if(_noDisposeArray[i].isChecked){
                    [chooseModel addObject:_noDisposeArray[i]];
                    [chooseAarmIds addObject:[NSNumber numberWithInteger:_noDisposeArray[i].efairydevicealarmstatistics_id]];
                }
                if(i>=150){
                    break;
                }
            }
            vc.chooseAlarmArray = chooseModel;
            vc.showAlarmIds =chooseAarmIds;
        }
    }
    vc.alarmType = _alarmType;
    vc.notDisposeIds = _noChooseIds;//不需要处理的报警信息id
    vc.datasCourceVC = 1;//数据传递来源 1代表从这本类传过去
    vc.successHandle = ^{
//        [self.tableView reloadData];
        [self.tableView.mj_header beginRefreshing];//自动下拉刷新
    };
    [self showViewController:vc sender:nil];
   
    
}

//点击了全选/全不选按钮
-(void)allCheckedBtnClick{
    //点击前是全选状态
    if(_isChooseAll){
        _isChooseAll = NO;
        _isOnClickChooseBtn = NO;
        for (AlarmNoDispostListModel *model in _noDisposeArray) {
            model.isChecked = NO;
        }
        _checkedRow = 0;
        [self setAllChooseBtnStatus:NO androws:_checkedRow];
    }else{
        _isChooseAll = YES;
        _isOnClickChooseBtn = YES;
        for (AlarmNoDispostListModel *model in _noDisposeArray) {
            model.isChecked = YES;
        }
        _checkedRow = _allRows;
        [self setAllChooseBtnStatus:YES androws:_allRows];
    }
    [_noChooseIds removeAllObjects];//移除点击了全选按钮然后又手动去消选择单个项的所有数据
    [self refreshWholeView];
}

//刷新数据
- (void)refreshWholeView {
    [self.tableView reloadData];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if([tableView isEqual:self.disTableView]){
        if(_disposeArray!=nil && _disposeArray.count > 0){
            return _disposeArray.count;
        }
    }else{
        if(_noDisposeArray!=nil && _noDisposeArray.count > 0){
            return 1;
        }
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([tableView isEqual:self.tableView]){
        if(_noDisposeArray!=nil && _noDisposeArray.count > 0){
            return _noDisposeArray.count;
        }
    }else if([tableView isEqual:self.disTableView]){
        return 1;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([tableView isEqual:self.tableView]){
        AlarmListCell *cell = [tableView dequeueReusableCellWithIdentifier:[@"AlarmListCell" description]];
        if(!cell){
            cell = [[AlarmListCell alloc]init];
//            cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);//隐藏分割新
        }
        
//        if(indexPath.row){
            [cell setModeDatas:_noDisposeArray[indexPath.row]];
//        }
        // 此步设置用于实现cell的frame缓存，可以让tableview滑动更加流畅
        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
        return cell;
    }else if([tableView isEqual:self.disTableView]){
        AlarmDisposeCell *cell = [tableView dequeueReusableCellWithIdentifier:[@"AlarmListCell" description]];
        if(!cell){
            cell = [[AlarmDisposeCell alloc]init];
            cell.layer.cornerRadius = 6;
            cell.layer.masksToBounds = YES;
//            cell.separatorInset = UIEdgeInsetsMake(8, 0, 0, 0);//上左下右 就可以通过设置这四个参数来设置分割线了

        }
        
//        if(indexPath.section){
            [cell setModeDatas:_disposeArray[indexPath.section]];
//        }
        // 此步设置用于实现cell的frame缓存，可以让tableview滑动更加流畅
        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
        return cell;
    }
    return nil;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if([tableView isEqual:self.disTableView]){
         return 8.f;
    }
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}



//设置header 的view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if([tableView isEqual:self.disTableView]){
         return [[UIView alloc] init];
    }
    return nil;
   
    
}

//设置footer 的view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if([tableView isEqual:self.disTableView]){
        return [[UIView alloc] init];
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
         return [tableView cellHeightForIndexPath:indexPath cellContentViewWidth:SCREEN_WIDTH tableView:tableView];

}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    cell.backgroundColor = [UIColor whiteColor];
//}

//接受点击cell item项
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row < self.deviceArray.count) {
//        [self jumpToDeviceInfoWithDeviceListItem:self.deviceArray[indexPath.row]];
//    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([tableView isEqual:self.tableView]){
        if(_noDisposeArray.count > 0){
             AlarmListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if(_noDisposeArray[indexPath.row].isChecked){
                _noDisposeArray[indexPath.row].isChecked = NO;
                _checkedRow--;
                _isChooseAll = NO;
                if(_isOnClickChooseBtn){
                    
                    
                    [_noChooseIds addObject:[NSNumber numberWithInteger:_noDisposeArray[indexPath.row].efairydevicealarmstatistics_id]];
                }
                cell.checkbox.image = cell.unCheckedImage;
               
                [self setAllChooseBtnStatus:NO androws:_checkedRow];
               
            }else{
                _checkedRow++;
                _noDisposeArray[indexPath.row].isChecked = YES;
                cell.checkbox.image = cell.checkedImage;
                [_noChooseIds removeObject:[NSNumber numberWithInteger:_noDisposeArray[indexPath.row].efairydevicealarmstatistics_id]];
                if(_checkedRow == _allRows){
                    _isChooseAll = YES;
                    [self setAllChooseBtnStatus:YES androws:_checkedRow];
                }else{
                     _isChooseAll = NO;
                    [self setAllChooseBtnStatus:NO androws:_checkedRow];
                }
            }
        }
       
    }else{
        AlarmDisposeDetailVC *vc = [[AlarmDisposeDetailVC alloc]init];
        vc.alarmId = _disposeArray[indexPath.section].efairydevicealarmstatistics_id;
        [self showViewController:vc sender:nil];
    }
   
}


-(void)setAllChooseBtnStatus:(BOOL)isChooseAll androws:(int)rows{
    if(isChooseAll){
        [_allChooseBtn setTitle:[NSString stringWithFormat:@"全不选(已选中%d条)",rows] forState:UIControlStateNormal];
        [_allChooseBtn setImage:[UIImage imageNamed:@"icon_checked"] forState:UIControlStateNormal];
    }else{
        [_allChooseBtn setTitle:[NSString stringWithFormat:@"全选(已选中%d条)",rows] forState:UIControlStateNormal];
        [_allChooseBtn setImage:[UIImage imageNamed:@"icon_no_checked"] forState:UIControlStateNormal];
    }
}

- (void)tableViewEndRefresh:(BOOL)isHeader {
    if (isHeader) {
        [self.tableView.mj_header endRefreshing];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
}

- (void)disTableViewEndRefresh:(BOOL)isHeader {
    if (isHeader) {
        [self.disTableView.mj_header endRefreshing];
    } else {
        [self.disTableView.mj_footer endRefreshing];
    }
}

#pragma mark - network request
- (void)requestNoDisposeListWithHeader:(BOOL)isHeader {
    // 发布Cos需要登录
    if (![EFAModel sharedInstance].isLogin) {
        [self tableViewEndRefresh:isHeader];
        return;
    }
  
    self.lastId = 0;
    if(!isHeader){
        if(_noDisposeArray && _noDisposeArray.count > 0){
            self.lastId = _noDisposeArray[_noDisposeArray.count-1].efairydevicealarmstatistics_id;
        }
    }
    
    NSMutableDictionary *pars = [NSMutableDictionary dictionary];

    [pars setObject:@([EFAModel sharedInstance].loginInfo.user_info.efairyuser_id) forKey:@"efairyuser_id"];
    [pars setObject:[EFAModel sharedInstance].loginInfo.access_token forKey:@"access_token"];
    //0全部，1火警，2预警，3故障
    if(_alarmType > 0){
        [pars setObject:@(_alarmType) forKey:@"alarm_id"];
    }
    [pars setObject:@(self.deviceId) forKey:@"efairydevice_id"];

    [pars setObject:@(self.lastId) forKey:@"last_id"];
    [pars setObject:@"20" forKey:@"size"];
    
    MBProgressHUD *hud = [MBProgressHUD xd_showHUDAddedTo:self.view animated:YES];
    [[XDAFNetworking sharedInstance] GET:[ApiManager apiAlarmNoDisposeList] parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求设备列表：%@", responseObject);
        [hud hideAnimated:NO];
        [self tableViewEndRefresh:isHeader];
        //把服务端返回的json格式数据解析为对象
        AlarmListModel *listModel = [AlarmListModel yy_modelWithDictionary:responseObject[EFAGlobalJsonKeyResult]];
        _allRows = (int)listModel.total_rows;
       // 服务端返回本次请求的项目列表数据
//        if(listModel.alarm_msg_list != nil){
            if(isHeader){
                //下拉刷新
                _noDisposeArray = listModel.alarm_msg_list;
            }else{
                //上啦加载追加数据到数组
                if(_isOnClickChooseBtn){
                    for (AlarmNoDispostListModel *item in listModel.alarm_msg_list) {
                        item.isChecked = YES;
                        [_noDisposeArray addObject:item];
                    }
                }else{
                    [_noDisposeArray addObjectsFromArray:listModel.alarm_msg_list];
                }
                
            }
            if(_noDisposeArray.count >= listModel.total_rows){
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer resetNoMoreData];
            }
//        }
        [self refreshWholeView];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求设备列表失败：%@", error);
        [self tableViewEndRefresh:isHeader];
        NSString *msg = [[ApiErrorCode sharedInstance] errorCodeMsgWithError:error];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:msg];
    }];
}

#pragma mark - network request 查询已处理数据
- (void)requestDisposeListWithHeader:(BOOL)isHeader {
    // 发布Cos需要登录
    if (![EFAModel sharedInstance].isLogin) {
        [self tableViewEndRefresh:isHeader];
        return;
    }
    self.disposeLastId = @"0";
    if(!isHeader){
        if(_disposeArray && _disposeArray.count > 0){
            self.disposeLastId = _disposeArray[_disposeArray.count-1].efairydevicealarmstatistics_id;
        }
    }
  
    NSMutableDictionary *pars = [NSMutableDictionary dictionary];
    
    [pars setObject:@([EFAModel sharedInstance].loginInfo.user_info.efairyuser_id) forKey:@"efairyuser_id"];
    [pars setObject:[EFAModel sharedInstance].loginInfo.access_token forKey:@"access_token"];
    //0全部，1火警，2预警，3故障
    if(_alarmType > 0){
        [pars setObject:@(_alarmType) forKey:@"alarm_id"];
    }
    [pars setObject:@(self.deviceId) forKey:@"efairydevice_id"];//设备编号
    
    [pars setObject:self.disposeLastId forKey:@"last_id"];//最后一条记录的编号
    [pars setObject:@"20" forKey:@"size"];//每页显示多少条数据

    MBProgressHUD *hud = [MBProgressHUD xd_showHUDAddedTo:self.view animated:YES];
    [[XDAFNetworking sharedInstance] GET:[ApiManager apiAlarmDisposeList] parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求设备列表：%@", responseObject);
        [hud hideAnimated:NO];
        [self disTableViewEndRefresh:isHeader];
        //把服务端返回的json格式数据解析为对象
        AlarmListModel *listModel = [AlarmListModel yy_modelWithDictionary:responseObject[EFAGlobalJsonKeyResult]];
        _disposeAllRows = (int)listModel.total_rows;
        
         //服务端返回本次请求的项目列表数据
//        if(listModel.alarmrecord_list != nil){
            if(isHeader){
                //下拉刷新
                _disposeArray = listModel.alarmrecord_list;
            }else{
                //上啦加载追加数据到数组
                [_disposeArray addObjectsFromArray:listModel.alarmrecord_list];
            }
            if(_disposeArray.count >= listModel.total_rows){
                [self.disTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.disTableView.mj_footer resetNoMoreData];
            }
//        }
        [self.disTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求设备列表失败：%@", error);
        [self disTableViewEndRefresh:isHeader];
        NSString *msg = [[ApiErrorCode sharedInstance] errorCodeMsgWithError:error];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:msg];
    }];
}





@end
